package test.n00200;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Problem00230 {
    public static void main(String[] args) {
        println(new ListNode[]{
                create(Arrays.asList(1, 4, 5)),
                create(Arrays.asList(1, 3, 4)),
                create(Arrays.asList(2, 6)),
        });
    }

    public static void println(ListNode[] lists) {
        Solution ss = new Solution();
        System.out.println(lists + "," + map(ss.mergeKLists(lists)));
    }

    private static ListNode create(List<Integer> a1) {
        ListNode[] list = new ListNode[a1.size()];
        for (int i = 0; i < a1.size(); i++) {
            list[i] = new ListNode(a1.get(i));
            if (i > 0) {
                list[i - 1].next = list[i];
            }
        }
        return list[0];
    }

    private static List<Integer> map(ListNode head) {
        if (head == null) {
            return Collections.emptyList();
        }
        List<Integer> list = new ArrayList<>();
        while (head != null) {
            list.add(head.val);
            head = head.next;
        }
        return list;
    }

    private static class Solution {

        /**
         * 给你一个链表数组，每个链表都已经按升序排列。
         * <p>
         * 请你将所有链表合并到一个升序链表中，返回合并后的链表。
         * <p>
         * 提示：
         * <p>
         * k == lists.length
         * 0 <= k <= 10^4
         * 0 <= lists[i].length <= 500
         * -10^4 <= lists[i][j] <= 10^4
         * lists[i] 按 升序 排列
         * lists[i].length 的总和不超过 10^4
         * <p>
         * 来源：力扣（LeetCode）
         * 链接：https://leetcode.cn/problems/merge-k-sorted-lists
         * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
         *
         * 解题思路:
         * 暴力
         *
         * @param lists
         * @return
         */
        public ListNode mergeKLists(ListNode[] lists) {
            ListNode head = new ListNode(0);
            ListNode prev = head;
            while (true) {
                int m = getMin(lists);
                if (m == 99999) {
                    return head.next;
                } else {
                    prev.next = new ListNode(m);
                    prev = prev.next;
                }
            }
        }

        public int getMin(ListNode[] lists) {
            int r = 99999;
            int cur = -1;
            for (int i = 0; i < lists.length; i++) {
                if (lists[i] != null && lists[i].val < r) {
                    r = lists[i].val;
                    cur = i;
                }
            }

            if (cur >= 0) {
                lists[cur] = lists[cur].next;
                return r;
            }
            return r;
        }
    }

    private static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
